********************************************************************************
** Cederman, Galano, Girardin and Schvitz. War Did Make States.
** Article prepared for International Organization
** June 20, 2022
**
** Stata do-file: data_prep_cent1.do
** Second data preparation file for state-level data (Centennia)
** Required file paths set in runall.do
********************************************************************************


cd $ROOT
cd $INTERMEDIATEDIR
use "statedata_intermediate.dta", clear 



merge 1:1 id year using dyad_cumul
drop _merge

merge 1:1 id year using cumulneigh
drop _merge



gen urban = populationurban/population
gen lurban = l5.urban
gen citystate = .
replace citystate = 0 if urban<=0.4 & llarea<8
replace citystate = 1 if urban>0.4 & llarea<8


gen warXsize = inc1*llarea

gen warXwargrowth = inc1*llwargrowth

gen warXpeacegrowth = inc1*llpeacegrowth

gen warXwarshrink = inc1*llwarshrink
gen warXpeaceshrink = inc1*llpeaceshrink

gen peaceXwargrowth = (1-inc1)*llwargrowth
gen peaceXpeacegrowth = (1-inc1)*llpeacegrowth
gen peaceXwarshrink = (1-inc1)*llwarshrink
gen peaceXpeaceshrink = (1-inc1)*llpeaceshrink


gen onsetinit1 = 0 
replace onsetinit1 = 1 if onsetinitiatorbrecke>0 & onsetinitiatorbrecke!=.

btscs onsetinit1 year id, gen(pys) nspline(3)
rename _spline1  pyspline1
rename _spline2  pyspline2
rename _spline3  pyspline3

gen onsetinitside1 = onsetinit1
replace onsetinitside1 = 1 if onsetfollowerbrecke == 1

btscs onsetinitside1 year id, gen(pys2) nspline(3)
rename _spline1  pys2pline1
rename _spline2  pys2pline2
rename _spline3  pys2pline3

gen incinit1 = 0
replace incinit1 = 1 if inc1>0 & incidenceinitiatorbrecke > 0 & incidenceinitiatorbrecke !=.

gen incinitside1 = incinit1
replace incinitside1 = 1 if incidencefollowerbrecke > 0 & incidencefollowerbrecke !=.

gen attacked1 = 0
replace attacked1 = 1 if onsetbrecke>0 & onsetinit1==0 & onsetinitside1==0

btscs attacked1 year id, gen(pyas) nspline(3)
rename _spline1  pyaspline1
rename _spline2  pyaspline2
rename _spline3  pyaspline3

gen incattacked1 = 0
replace incattacked1 = 1 if incidencebrecke>0 & incinit1==0 & incinitside1==0

///////////////////////////////////////////////////////////////////////////////////////////

bys id (year): gen warsum1 = sum(inc1)
gen lwarsum1 = l5.warsum1
gen llwarsum1 = log(lwarsum1+1)
bys id (year): gen warsum = sum(incidencebrecke)
gen lwarsum = l5.warsum
gen llwarsum = log(lwarsum+1)

bys id (year): gen onsetinitsum = sum(onsetinit1)
gen lonsetinitsum = l5.onsetinitsum
gen llonsetinitsum = log(lonsetinitsum + 1)


///////////////////////////////////////////////////////////////////////////////////////////



bys year: egen warring = sum(inc1)
xtset id year
gen llcoastdist = log(l5.coastmin + 1)
gen lcoastal = 0
replace lcoastal = 1 if l5.coastmin < 10


gen lwarring = l5.warring
*gen lsharewarring = (l5.warring/l5.n)
replace elevationsd = 0 if elevationsd == . 

gen llelevsd = log(1 + l5.elevationsd)


cd $ROOT
cd $INTERMEDIATEDIR
save "statedata.dta", replace 
